// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

define(['../Core'], function (Core) {
    var Fade = function(objAnimation) {
        // Inherit properties
        Core.InheritProperties(objAnimation, this);
        this.prototype = Object.create(objAnimation.__proto__);
    };

    var _Fade = function() {};

    _Fade.prototype = {
        Fade: function(el, self, currentElement, maxElements) {
            // It is possible that element doesn't have
            // opacity defined
            if(String.prototype.isEmpty(el.style.opacity)) {
                el.style.opacity = 1;
                requestAnimationFrame(function() {
                    self.Trigger(el, currentElement, maxElements);
                }, null);
            } else {
                // Start decrementing element's opacity
                if(el.style.opacity > 0) {
                    el.style.opacity -= (1 / 60) * (1000 / self.Duration);
                    requestAnimationFrame(function() {
                        self.Trigger(el, currentElement, maxElements);
                    }, null);
                } else {
                    // If RemoveElement, remove it
                    // from DOM
                    if(self.RemoveElement) {
                        document.body.removeChild(el);
                    }

                    // Current element is used to determine if
                    // we should call a callback or not
                    return currentElement;
                }
            }
        }
    };

    Fade.prototype = {
        Create: function(objAnimation) {
            return new Fade(objAnimation);
        },

        /**
         * @return {number}
         */
        Trigger: function(el, currentElement, maxElements) {
            var _i, _length;
            var self = this;

            // Define controls argument if undefined
            if(typeof currentElement === 'undefined' || typeof maxElements === 'undefined') {
                currentElement = 0;
                maxElements = 0;
            }

            // Our argument can be a HTMLCollection, which is treated
            // a bit differently
            if(Object.prototype.toString.call(el) === '[object HTMLCollection]') {
                requestAnimationFrame(function() {
                    // Set maxElements as our controller for
                    // further comparison which defines if we
                    // should call a callback
                    maxElements = el.length;
                    for(_i = 0, _length = el.length - 1; _i <= _length; _i = _i + 1) {
                        _Fade.prototype.Fade(el[_i], self, _i+1, maxElements);
                    }
                }, null);
            } else {
                requestAnimationFrame(function() {
                    var result;
                    result = _Fade.prototype.Fade(el, self, currentElement, maxElements);

                    if(result == maxElements) {
                        self.prototype.Stop(self.Callback);
                    }
                }, null);
            }
        }
    };

    return Fade.prototype.Create;
});